package com.prs.crm.action.oa.bookmanager;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.annotation.Clew;
import com.prs.crm.domain.oa.bookmanager.BookType;
import com.prs.crm.service.oa.bookmanager.BookTypeService;

public class BookTypeAction  extends BaseAction{

	private static final long serialVersionUID = 1L;
	private BookType bookType;

	private BookTypeService bookTypeService;
	
	private PaginationSupport pagination;
	
	private Integer[] ids;
	
	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public BookType getBookType() {
		return bookType;
	}

	public void setBookType(BookType bookType) {
		this.bookType = bookType;
	}

	public BookTypeService getBookTypeService() {
		return bookTypeService;
	}

	public void setBookTypeService(BookTypeService bookTypeService) {
		this.bookTypeService = bookTypeService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	
	@Action("listBookType")
	public String listBookRegister(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(BookType.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getBookTypeService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	@Action("editBookType")
	public String editBookType(){
		return SUCCESS;
	}
	
	@Clew(results = { "success", "none" })
	@Action(value = "saveBookType", results = {
			@Result(name = "success", type = "redirect", location = "listBookType"),
			@Result(name = "error", type = "chain", location = "editBookType"),
		 })
	public String saveBookType(){
		this.getBookTypeService().save(bookType);
		this.setPrompt("添加成功！");
		return SUCCESS;
	}
	@Action(value = "removeBookType", results = {
			@Result(name = "success", type = "redirect", location = "listBookType"),
			@Result(name = "error", type = "chain", location = "listBookType") })
	public String removeBookType(){
		if (ids==null) {
			this.addActionError("请选中一行进行操作！");
			return ERROR;
		}
		
		for (Integer id : ids) {
			BookType bt=this.getBookTypeService().get(id);
			if (bt.getBookRegister().size()!=0) {
				this.addActionError("该数据有关联不能删除！");
				return SUCCESS;
			}
		}
		this.getBookTypeService().batchRemove(ids);
		return SUCCESS;
	}
}
